package org.dreamwork.drawing.framework;

import java.awt.*;

public class BoxFigure extends Figure {
    public BoxFigure (DrawingCanvas canvas) {
        super (canvas);
    }

    protected void drawSelectionHandler (Graphics g) {
        Color old = g.getColor ();
        g.setColor (Color.BLACK);
        // draw west-north handler
        int left = r.x - HANDLE_SIZE;
        int top  = r.y - HANDLE_SIZE;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw middle-north handler
        left = r.x + (r.width - HANDLE_SIZE) / 2;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw east-north handler
        left = r.x + r.width;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw west-center handler
        left = r.x - HANDLE_SIZE;
        top = r.y + (r.height - HANDLE_SIZE) / 2;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw east-center handler
        left = r.x + r.width;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw west-south handler
        left = r.x - HANDLE_SIZE;
        top = r.y + r.height;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw middle-south handler
        left = r.x + (r.width - HANDLE_SIZE) / 2;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        // draw east-south handler
        left = r.x + r.width;
        g.fillRect (left, top, HANDLE_SIZE, HANDLE_SIZE);
        g.setColor (old);
    }

    public void paint (Graphics g) {
        g.setColor (background);
        g.fillRect (r.x, r.y, r.width, r.height);
        g.setColor (border);
        g.drawRect (r.x, r.y, r.width, r.height);

        if (selected) drawSelectionHandler (g);
    }

    public void resizeTo (int width, int height) {
        Graphics g = getGraphic ();
        clear (g);
        r.setSize (width, height);
        paint (g);
    }

    public void resizeBy (int widthOffset, int heightOffset) {
        Graphics g = getGraphic ();
        clear (g);
        r.setSize (r.width + widthOffset, r.height + heightOffset);
        paint (g);
    }

    protected void clear (Graphics g) {
        g.setColor (canvas.getBackground ());
        g.fillRect (r.x, r.y, r.width, r.height);
    }

    public Point getCenter () {
        return new Point (r.x + r.width / 2, r.y + r.height / 2);
    }

    public Dimension getPreferredSize () {
        return getSize ();
    }
}